<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\lose\controller;

use cmf\controller\HomeBaseController;
use app\lose\model\LoseCategoryModel;
use think\Db;

class ListController extends HomeBaseController
{
    public function index()
    {
        $img = Db::name('img')->find(5);
        $this->assign('img',$img);
        $parent_id = input('id', 0, 'intval');

        $loseCategoryModel = new LoseCategoryModel();
        $category            = $loseCategoryModel->where('parent_id', $parent_id)->find();

       
        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];
        // pre($listTpl,1);die;

        $this->assign('category', $category);

        //列表

        $join = [
            ['lose_category_post cp', 'p.id = cp.post_id'],
            ['lose_category c', 'c.id = cp.category_id']
        ];

        $lists = Db::name('lose_post')
        ->alias("p")
        ->join($join)
        ->where("cp.is_show=1 and p.delete_time=0")
        ->field('p.*')
        ->order("p.id desc")
        ->limit(6)
        ->select()
        ->toArray();

        $this->assign('lists', $lists);

        // pre($lists);die;

        
        
        $request = request();
        if( $request->isAjax() ){
            $html = $this->fetch($listTpl.'_ajax');
            
            $return = array(
                // 'error'     => 0,
                'data'      => $html,
                // 'start'     => $start,
                // 'size'      => $size,
                // 'more_data' => empty($more_data) ? 0 : 1 ,
            );
            return $return;
        }
        return $this->fetch('/' . $listTpl);
    }

    //ajax加载更多，查询该分类下的文章
    public function listAjax(){
        $cid      = input('cid',0,'intval');
        $start    = input('start',0,'intval');
        $size     = input('size',0,'intval');
        $list_tpl = input('list_tpl','','trim');


        $join = [
            ['lose_category_post cp', 'p.id = cp.post_id'],
            ['lose_category c', 'c.id = cp.category_id']
        ];

        $lists = Db::name('lose_post')
        ->alias("p")
        ->join($join)
        ->where("c.id=".$cid." and cp.is_show=1 and p.delete_time=0")
        ->order("p.id desc")
        ->field('p.id,p.post_title,p.create_time,p.receive_place,p.create_time')
        ->select()
        ->toArray();

        // pre($lists);die;

        foreach ($lists as $k => $v) {
            $lists[$k]['create_time'] = date('m-d',$lists[$k]['create_time']);
        }

        // pre($lists,1);

        /*//没有数据
        if( count($lists) < 1 ){
            $return = array(
                'error'   => 1,
            );
            return $return;
        }
        */

        // pre($lists);die;
        return $lists;
    }

    //详情页面
    public function detail(){

        $img = Db::name('img')->find(5);
        $this->assign('img',$img);
        
        $id = input('id', 0, 'intval');

        $info = db('lose_post')->where('id='.$id)->find();

        $info['post_content'] = html_entity_decode($info['post_content']);

        $this->assign('info', $info);

        return $this->fetch('/detail');
    }

}
